//document.write('<script src="/assets/modernizr/modernizr.js"></script>');
//document.write('<script src="/assets/underscore/underscore.js"></script>');
//document.write('<script src="/assets/jquery-migrate/jquery-migrate.min.js"></script>');
//document.write('<script src="/assets/jquery-ui-bootstrap/assets/js/jquery-ui-1.10.0.custom.min.js"></script>');
//document.write('<script src="/assets/Placeholders.js/placeholders.jquery.min.js"></script>');
//document.write('<script src="/assets/select2/select2.js"></script>');
//document.write('<script src="/assets/jquery.validation/dist/jquery.validate.js"></script>');
//document.write('<script src="/assets/jquery.validation/src/localization/messages_zh.js"></script>');
//document.write('<script src="/assets/jquery.uniform/jquery.uniform.js"></script>');
//document.write('<script src="/assets/noty/js/noty/jquery.noty.js"></script>');
//document.write('<script src="/assets/noty/js/noty/layouts/top.js"></script>');
//document.write('<script src="/assets/noty/js/noty/themes/default.js"></script>');
//document.write('<script src="/assets/app/jquery-ujs.js"></script>');
//document.write('<script src="/assets/app/application.js"></script>');
document.write('<script src="/assets/fileUpload/ajaxfileupload.js"></script>');
document.write('<script src="/assets/fileUpload/upload.js"></script>');
//document.write('<script src="/assets/zTree_v3/js/jquery.ztree.core-3.5.js"></script>');
document.write('<script src="/assets/daterangepicker/moment.min.js"></script>');
document.write('<script src="/assets/daterangepicker/daterangepicker.js"></script>');
document.write('<script src="/assets/loading/loading.js"></script>');
document.write('<script src="/assets/jquery.cookie/cookie.js"></script>');
document.write('<script src="/assets/iCheck/icheck.min.js"></script>');
//document.write('<script src="/assets/input-mask/jquery.inputmask.js"></script>');
document.write('<script src="/assets/jquery.backstretch/jquery.backstretch.min.js"></script>');

document.write('<script src="/assets/wizard/jquery.bootstrap.wizard.min.js"></script>');
document.write('<script src="/assets/tool.js"></script>');

// 获取长度为len的随机字符串
function _getRandomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz+-*?!2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
    var maxPos = $chars.length;
    var pwd = "";
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
/**
 * 刷新验证码图片
 * @param imgObj
 */
function refreshCode(imgObj){
    if (!imgObj) {
        imgObj = document.getElementById("validationCodeImg");
    }
    var index = imgObj.src.indexOf("?");
    if(index != -1) {
        var url = imgObj.src.substring(0,index + 1);
        imgObj.src = url + Math.random();
    } else {
        imgObj.src = imgObj.src + "?" + Math.random();
    }
}

$.ajaxSetup({
    contentType:"application/x-www-form-urlencoded;charset=utf-8",
    complete:function(XMLHttpRequest,textStatus){
        if(XMLHttpRequest.getResponseHeader){
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus"); //通过XMLHttpRequest取得响应头，sessionstatus，
            if(sessionstatus=="timeout"){
                alert("您长时间未操作，请重新登录！");
                //如果超时就处理 ，指定要跳转的页面
                //window.location.replace(webPath.webRoot + "account/login");
            }
        }
    }
});

$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

var DaiwoniuPagination = function(){
    var aa = {
        paginationDomId: 'pagination',
        ajaxUrl: undefined,
        ajaxType: 'GET',
        ajaxDataType: 'json',
        page: 1,
        pageSize: 10,
        totalPage: 0,
        totalSize: 0,
        dataList: [],
        queryData: {},
        requestBefore: function(){},
        requestAfter: function(){},
        showHandle: function (dataList) {
        },
        init: function (setting) {
            this.ajaxUrl = setting.url;
            if (setting.paginationDomId) {
                this.paginationDomId = setting.paginationDomId;
            }
            if(setting.showHandle){
                this.showHandle = setting.showHandle;
            }
            if(setting.requestBefore){
                this.requestBefore = setting.requestBefore;
            }
            if(setting.requestAfter){
                this.requestAfter = setting.requestAfter;
            }
            if(setting.pageSize){
                this.pageSize = setting.pageSize;
            }
            this.totalPage = 0;
            this.totalSize = 0;
            this.dataList = [];
            return this;
        },
        setPaginationDomId: function (paginationDomId) {
            this.paginationDomId = paginationDomId;
        },
        paginationDomInit: function () {
            if (this.totalSize <= 0) {
                var domObj = $("#" + this.paginationDomId);
                if (domObj) {
                    domObj.html("");
                }
                return;
            }
            if (this.paginationDomId) {
                var domObj = $("#" + this.paginationDomId);
                if (domObj) {
                    var leftHtml = "";
                    //leftHtml = leftHtml + '<div class="col-sm-5">';
                    ////leftHtml = leftHtml + '<div class="dataTables_info" id="example1_info" role="status" aria-live="polite">Showing 1 to 10 of 57 entries</div>';
                    //leftHtml = leftHtml + '</div>';
                    var rightHtml = "";
                    //rightHtml = rightHtml + '<div class="col-sm-7">';
                    rightHtml = rightHtml + '<div class="dataTables_paginate text-right">';
                    rightHtml = rightHtml + '<div>';
                    rightHtml = rightHtml + '<input type="hidden" name="totalSize" value="'+this.totalSize+'">';
                    rightHtml = rightHtml + '<button type="button" class="btn btn-sm btn-default" id="pagination_last"><i class="fa fa-caret-left"></i></button>&nbsp;';
                    rightHtml = rightHtml + '<span>' + this.page + '/' + this.totalPage + '</span>&nbsp;';
                    rightHtml = rightHtml + '<button type="button" class="btn btn-sm btn-default" id="pagination_next"><i class="fa fa-caret-right"></i></button>&nbsp;&nbsp;';
                    rightHtml = rightHtml + '<input type="text" class="input-sm" style="width: 50px;" id="pagination_to_input" value="' + this.page + '" /> &nbsp;';
                    rightHtml = rightHtml + '<button type="button" class="btn btn-sm btn-default" id="pagination_to_btn">&nbsp;跳转&nbsp;</button>';
                    rightHtml = rightHtml + '</div>';
                    rightHtml = rightHtml + '</div>';
                    //rightHtml = rightHtml + '</div>';
                    domObj.html(leftHtml + rightHtml);

                    var pageThis = this;
                    //绑定事件
                    $("#pagination_to_btn").on("click", function () {
                        var toPage = $("#pagination_to_input").val();
                        if (isNaN(toPage) || toPage < 1 || toPage > pageThis.totalPage) {
                            alert("必须输入最大页数内的正整数");
                            return;
                        }
                        if (toPage == pageThis.page) {
                            return;
                        }
                        pageThis.showPage(undefined, toPage);
                    });
                    $("#pagination_last").on("click", function () {
                        pageThis.lastPage(undefined);
                    });
                    $("#pagination_next").on("click", function () {
                        pageThis.nextPage(undefined);
                    });
                    if (this.page <= 1) {
                        $("#pagination_last").addClass("disabled");
                    }
                    if (this.page >= this.totalPage) {
                        $("#pagination_next").addClass("disabled");
                    }
                }
            }
        },
        /**
         * 根据检索条件获取显示数据
         * @param queryData 检索条件，不用传入页数（queryData=undefined 使用上一次的检索条件，queryData=null||queryData={} 使用空的检索条件）
         */
        show: function (queryData) {
            if (this.page < 1) {
                alert("已经到第一页了");
                this.page = 1;
                return;
            }
            if (this.page > 1 && this.page > this.totalPage) {
                alert("已经到最后一页了");
                this.page = this.totalPage;
                return;
            }
            if (queryData && queryData != null) {
                //queryData.page = this.page;
            } else if (queryData == undefined) {
                queryData = this.queryData;
            } else {
                queryData = {};
            }
            queryData.page = this.page;
            queryData.pageSize = this.pageSize;
            this.queryData = queryData;
            this.dataList = [];
            var pageThis = this;
            this.requestBefore();
            $.ajax({
                url: this.ajaxUrl,
                type: this.ajaxType,
                data: this.queryData,
                dataType: this.ajaxDataType,
                async: true,
                cache: false,
                success: function (returndata) {
                    pageThis.requestAfter();
                    if (returndata && returndata.status == "true") {
                        var pagination = returndata.pagination;
                        if (pagination) {
                            pageThis.totalSize = parseInt(pagination.total);
                            pageThis.totalPage = parseInt(pagination.totalPages);
                            pageThis.dataList = pagination.data;
                        }
                        pageThis.showHandle(pageThis.dataList);
                        pageThis.paginationDomInit();
                    } else {
                        console.log(returndata);
                        alert("获取列表失败");
                    }
                },
                error: function (returndata) {
                    pageThis.requestAfter();
                    console.log(returndata);
                    alert("获取列表异常：");
                }
            });
        },
        showPage: function (queryData, page) {
            this.page = page;
            this.show(queryData);
        },
        lastPage: function (queryData) {
            this.page = this.page - 1;
            this.show(queryData);
        },
        nextPage: function (queryData) {
            this.page = this.page + 1;
            this.show(queryData);
        }
    }
    return aa;
};

var setDateDom = function(dom, isSingle){
    if(dom){
        if(isSingle != false){
            isSingle = true;
        }
        var format = dom.attr("format");
        if(!format){
            format = "YYYY-MM-DD";
        }
        if(isSingle != true){
            var startDate = dom.attr("startDate");
            if(!startDate){
                startDate = "2016-5-22";
            }
            var endDate = dom.attr("endDate");
            if(!endDate){
                endDate = "2016-6-22";
            }
            dom.daterangepicker({
                opens: 'left',
                parentEl: '',
                startDate: startDate,
                endDate: endDate,
                drops: 'down',
                buttonClasses: 'btn',
                //applyClass: '',
                //cancelClass: '',
                locale: {
                    format: format,
                    applyLabel: '确定',
                    cancelLabel: '取消',
                    monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
                    daysOfWeek: ["一","二","三","四","五","六","日"]
                }
            });
        }else{
            var initDate = dom.attr("initDate");
            if(!initDate){
                initDate = "2016-5-22";
            }
            dom.daterangepicker({
                startDate: initDate,
                singleDatePicker: true,
                showDropdowns: true,
                autoApply: false,
                locale: {
                    format: format,
                    monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
                    daysOfWeek: ["一","二","三","四","五","六","日"]
                }
            });
        }
    }
};
var setDateDomByClass = function(domClass, isSingle){
    if(domClass){
        var domList = $("." + domClass);
        if(domList){
            $.each(domList, function(index, dom){
                setDateDom($(dom), isSingle);
            });
        }
    }
};


$(function(){
    $("#validationCodeImg").on("click", function(){
        refreshCode(this);
    });
    $(".validationCodeRefresh").on("click", function(){
        refreshCode(null);
    });

    setDateDomByClass("daterangepicker_class", true);
});